//
//  SeparateDetailsCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/14.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "SeparateDetailsCell.h"

@implementation SeparateDetailsCell

- (void)setupUI{
    
    self.backgroundColor = KWhiteColor;
    
    [self.contentView addSubview:self.shopIcon];
    [self.contentView addSubview:self.pigTypeTipLab];
    [self.contentView addSubview:self.foldLabel];
    [self.contentView addSubview:self.newsText];
    [self.contentView addSubview:self.bottomSpaceView];

}

- (void)setupLayout
{
    [self.shopIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.width.height.mas_equalTo(68);
    }];
    
    [self.pigTypeTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.shopIcon.mas_right).mas_offset(13);
        make.top.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(22);
    }];

    
    [self.bottomSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.newsText.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.newsText.mas_left);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
    }];

    
    [self.newsText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.shopIcon.mas_right).mas_offset(13);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(self.shopIcon.top).mas_offset(43);
    }];
    
    self.newsText.preferredMaxLayoutWidth = KScreenWidth - 110;
    
    [self.foldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_right).mas_offset(-75);
        make.width.mas_equalTo(55);
        make.height.mas_equalTo(16);
        make.top.mas_equalTo(self.newsText.mas_bottom).offset(-16);
    }];
    
}

- (void)setItem:(PigpositionList *)item{
    
    {
        self.newsText.text = item.positionContent;
        
        NSRange range = [self getTwoTextRangeWith: item.positionContent];
        NSString *aimsText = [NSString stringWithFormat:@"%@...", [item.positionContent substringToIndex:range.length]];
    
        if (self.newsText.text.length > 0) {
            NSMutableAttributedString *img_text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", item.positionContent]];
            
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            [paragraphStyle setLineSpacing:3];
            [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
            [img_text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.newsText.text.length)];
            
            self.newsText.attributedText = img_text;
        }
        
        CGFloat contentW = KScreenWidth -110 ;
        NSString *contentStr = self.newsText.text;
        
        NSMutableParagraphStyle *descStyle = [[NSMutableParagraphStyle alloc]init];
        [descStyle setLineSpacing:3];//行间距
        
        CGRect textRect = [contentStr
                           boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:@{NSFontAttributeName : mFont(12), NSParagraphStyleAttributeName : descStyle}
                           context:nil];
        
        if (textRect.size.height > 30) {
            
            self.foldLabel.hidden = NO;
            if (item.isOpening) {
                self.newsText.numberOfLines = 0;
                self.foldLabel.text = @"收起";
            }else{
                
                self.newsText.numberOfLines = 2;
                self.newsText.text = aimsText;
                
                self.foldLabel.text = @"查看全部";
            }
        }else{
            
            self.newsText.numberOfLines = 0;
            self.foldLabel.hidden = YES;
        }

    }
    
    {
        [_shopIcon sd_setImageWithURL:[NSURL URLWithString:item.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_small"]];
    }
    
    {
        _pigTypeTipLab.text = item.positionName;
    }
}

/**
 
  返回两行文本的range
 
  */

- (NSRange)getTwoTextRangeWith:(NSString *)string {
    
      NSMutableDictionary * attributes = [NSMutableDictionary dictionaryWithCapacity:5];
    
      UIFont * font = mFont(14);
    
      [attributes setValue:font forKey:NSFontAttributeName];
    
      NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString:string/*需要分页的字符串*/ attributes:attributes];
    
      CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) attributedString);
    
      UIBezierPath * bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, KScreenWidth - 110, 42)];
    
      CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), bezierPath.CGPath, NULL);
    
      CFRange range = CTFrameGetVisibleStringRange(frame);
    
      CFRelease(frame);
    
      CFRelease(framesetter);
    
          
       NSRange rg = NSMakeRange(range.location, range.length);
    
       return rg;
    
}



- (UIImageView *)shopIcon{
    if (!_shopIcon) {
        _shopIcon = [[UIImageView alloc] init];
        CAShapeLayer *layer = [CAShapeLayer layer];
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 68, 68) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
        layer.path = path.CGPath;
        _shopIcon.layer.mask = layer;
        _shopIcon.image = [UIImage imageNamed:@"placeholder_small"];
    }
    return _shopIcon;
}


- (UILabel *)pigTypeTipLab{
    
    if (! _pigTypeTipLab){
        _pigTypeTipLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x262626) font:mFont(16) textAliment:0];
    }
    return _pigTypeTipLab;
}


- (UILabel *)newsText{
    
    if (!_newsText){
        _newsText = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x808080) font:mFont(12) textAliment:0];
        _newsText.lineBreakMode = NSLineBreakByTruncatingTail;
        _newsText.numberOfLines = 2;
    }
    return _newsText;
}


- (UILabel *)foldLabel{
    
    if (!_foldLabel) {
        _foldLabel = [UILabel makeLabelWithText:@"" textColor:mMainColor font:mFont(12) textAliment:2];
        _foldLabel.backgroundColor = KWhiteColor;
        _foldLabel.hidden = YES;
        
        @weakify(self);
        _foldLabel.tapBlock = ^{
            
            @strongify(self);
            if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(clickFoldLabel:)]) {
                [self.cellDelegate clickFoldLabel:self];
            }
        };
    }
    return _foldLabel;
}


- (UIView *)bottomSpaceView {
    
    if (!_bottomSpaceView){
        _bottomSpaceView = [[UIView alloc] init];
        _bottomSpaceView.backgroundColor = UIColorFromRGB(0xF2F2F2);
    }
    return _bottomSpaceView;
}


@end
